package com.lezheba.api;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.util.JSONBuilder;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.lezheba.action.BaseAction;
import com.lezheba.db.entity.Category;
import com.lezheba.db.entity.Deal;
import com.lezheba.db.entity.Shop;
import com.lezheba.service.IndexService;

public class JsonApi extends BaseAction {
	private static Logger log = Logger.getLogger(JsonApi.class);

	private static final long serialVersionUID = 3828494970421292037L;

	private long dealId = -1;

	public long getDealId() {
		return dealId;
	}

	public void setDealId(long dealId) {
		this.dealId = dealId;
	}

	private String cityName = "";
	private String cityId = "";


	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public String getCityName() {
		return cityName;
	}



	public void setCityName(String cityName) {
		
		this.cityName = super.getAfterTranslate(cityName);
	}

	public long getCateId() {
		return cateId;
	}

	public void setCateId(long cateId) {
		this.cateId = cateId;
	}

	private long cateId;
	private int page = 1;
	private int limit = 10; // page number

	public String category() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		IndexService service = new IndexService();
		List<Category> list = service.getCategoryList();
		JSONBuilder builder = new JSONBuilder(response.getWriter());
		builder.array();
		for (Category category : list) {
			builder.object();
			builder.key("id").value(category.getId());
			builder.key("name").value(category.getName());
			builder.endObject();
		}
		builder.endArray();
		response.getWriter().close();
		return SUCCESS;
	}

	public String shop() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		IndexService service = new IndexService();
		Shop shop = service.getShop(dealId);
		JSONBuilder builder = new JSONBuilder(response.getWriter());

		builder.object();
		if (shop != null) {
			builder.key("id").value(shop.getId());
			builder.key("deal_id").value(shop.getDealId());
			builder.key("shop_addr").value(shop.getShopAddr());
			builder.key("shop_area").value(shop.getShopArea());
			builder.key("shop_dpid").value(shop.getShopDpid());
			builder.key("shop_lat").value(shop.getShopLat());
			builder.key("shop_long").value(shop.getShopLong());
			builder.key("shop_name").value(shop.getShopName());
			builder.key("shop_tel").value(shop.getShopTel());
			builder.key("shop_trafficinfo").value(shop.getShopTrafficinfo());
		}

		builder.endObject();

		response.getWriter().close();
		return SUCCESS;
	}

	public String deal() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		IndexService service = new IndexService();
		List<Deal> list = null;
		if(cityId != null && cityId.length()>0){
			list = service.getDealListByCityId(cityId, cateId, page, limit);
		}else{
			list = service.getDealList(cityName, cateId, page, limit);
		}
		
		JSONBuilder builder = new JSONBuilder(response.getWriter());
		builder.array();
		for (Deal deal : list) {
			builder.object();
			builder.key("id").value(deal.getId());
			builder.key("websiteId").value(deal.getWebsiteId());
			builder.key("city_name").value(deal.getCityName());
			builder.key("city_url").value(deal.getCityUrl());
			builder.key("dealId").value(deal.getDealId());
			builder.key("dealTitle").value(deal.getDealTitle());
			builder.key("deal_img").value(deal.getDealImg());
			builder.key("deal_cate_id").value(deal.getDealCateId());
			builder.key("deal_subcate_id").value(deal.getDealSubcateId());
			builder.key("deal_desc").value(deal.getDealDesc());
			builder.key("value").value(deal.getValue());
			builder.key("price").value(deal.getPrice());
			builder.key("rebate").value(deal.getRebate());
			builder.key("sales_min").value(deal.getSales_min());
			builder.key("sales_num").value(deal.getSalesNum());
			builder.key("sold_out").value(deal.getSoldOut());
			builder.key("is_post").value(deal.isPost());
			builder.key("start_time").value(deal.getStartTime());
			builder.key("end_time").value(deal.getEndTime());
			builder.key("coupon_start_time").value(deal.getCouponStartTime());
			builder.key("coupon_end_time").value(deal.getCouponEndTime());
			builder.key("deal_tips").value(deal.getDealTips());
			builder.key("deal_wow").value(deal.getDealWow());
			builder.endObject();
		}
		builder.endArray();
		response.getWriter().close();
		return SUCCESS;
	}

	public String dealtotal() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		IndexService service = new IndexService();
		JSONBuilder builder = new JSONBuilder(response.getWriter());

		builder.object();
		builder.key("dealtotal").value(service.getDealtotal(cityName, cateId));
		builder.endObject();

		response.getWriter().close();
		return SUCCESS;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

}
